var simpleAlert = function (opts) {
    //设置默认参数
    var opt = {
        "closeAll": false,//关闭上级所有弹框
        "title": "",//标题
        "buttons": {},//自定义按钮
        "icon": "",//添加图标
        "drop": false,//是否拖拽
        "showTitle": true,//是否显示标题
        "width": "20%"//自定义宽度
    }
    //合并参数
    var option = $.extend(opt, opts);
    if (option.showTitle && !option.title) {
        option.title = "提示";
    }
    //事件
    var dialog = {}
    var $simpleAlert = $('<div class="simpleAlert">');
    var $shelter = $('<div class="simpleAlertShelter">');
    var $simpleAlertBody = $('<div class="simpleAlertBody" style="width:' + option.width + '">');
    var $headTitle = $('<div id="headTitle"></div>')
    var $simpleAlertBodyClose = $('<div class="simpleAlertBodyClose"><i class="mdi mdi-close"></i></div>');
    var $simpleAlertBodyIcon;
    var $simpleAlertBodyFooter = $('<div class="simpleAlertBodyFooter"></div>');
    var $simpleAlertBodyBox = $('<div class="simpleAlertBodyBox"></div>');
    var $simpleAlertBodyContent = $('<p class="simpleAlertBodyContent">' + option.content + '</p>');
    if (option.icon) {
        var $simpleAlertBodyIcon = $('<img class="simpleAlertBodyIcon" src=' + (option.icon == 'success' ? '../images/czcg.png' : '../images/czsb.png') + '  />');
    }
    if (option.title) {
        var $simpleAlertBodyTitle = $('<p class="simpleAlertBodyTitle">' + option.title + '</p>');
    }
    dialog.init = function () {
        $headTitle.append($simpleAlertBodyTitle).append($simpleAlertBodyClose)
        $simpleAlertBody.append($headTitle).append($simpleAlertBodyBox.append($simpleAlertBodyIcon).append($simpleAlertBodyContent))
        var num = 0;
        var only = false;
        var onlyArr = [];
        for (var i = 0; i < 2; i++) {
            for (var key in option.buttons) {
                switch (i) {
                    case 0:
                        onlyArr.push(key);
                        break;
                    case 1:
                        if (onlyArr.length <= 1) {
                            only = true;
                        } else {
                            only = false;
                        }
                        num++;
                        var $btn = $('<button class="simpleAlertBtn simpleAlertBtn' + num + ' ' + option.buttons[key].type + '">' + key + '</button>')
                        $btn.bind("click", option.buttons[key].fun);

                        if (only) {
                            $btn.addClass("onlyOne")
                        }
                        $simpleAlertBodyFooter.append($btn);
                        break;
                }

            }
        }
        $simpleAlertBody.append($simpleAlertBodyFooter);
        $simpleAlert.append($shelter).append($simpleAlertBody);
        $("body").append($simpleAlert);
        $simpleAlertBody.show().animate({ "opacity": "1" }, 300);
    }
    // 右上角关闭按键事件
    $simpleAlertBodyClose.bind("click", function () {
        option.closeAll = false;
        dialog.close();
    })
    dialog.close = function () {
        if (option.closeAll) {
            $(".simpleAlertBody").animate({ "marginTop": "-188px", "opacity": "0" }, 200, function () {
                $(".simpleAlert").remove()
            });
            $(".simpleAlertShelter").animate({ "opacity": "0" }, 200);
        } else {
            $simpleAlertBody.animate({ "marginTop": "-188px", "opacity": "0" }, 200, function () {
                $(".simpleAlert").last().remove()
            });
            $shelter.animate({ "opacity": "0" }, 200);
        }
    }
    dialog.init();
    if (option.drop) {
        $('#headTitle').css({
            'cursor': 'move'
        })
        $('#headTitle').mousedown(function (e) {
            var positionDiv = $(this).offset();
            var distenceX = e.pageX - positionDiv.left - $('#headTitle').outerWidth(true) / 2;
            var distenceY = e.pageY - positionDiv.top - $('.simpleAlertBody').outerHeight(true) / 2;

            $(document).mousemove(function (e) {
                var x = e.pageX - distenceX;
                var y = e.pageY - distenceY;
                if (x < $('.simpleAlertBody').outerWidth(true) / 2) {
                    x = $('.simpleAlertBody').outerWidth(true) / 2;
                } else if (x > $(document).width() - $('.simpleAlertBody').outerWidth(true) / 2) {
                    x = $(document).width() - $('.simpleAlertBody').outerWidth(true) / 2;
                }
                if (y < $('.simpleAlertBody').outerHeight(true) / 2) {
                    y = $('.simpleAlertBody').outerHeight(true) / 2;
                } else if (y > $(document).height() - $('.simpleAlertBody').outerHeight(true) / 2) {
                    y = $(document).height() - $('.simpleAlertBody').outerHeight(true) / 2;
                }
                $('.simpleAlertBody').css({
                    'left': x + 'px',
                    'top': y + 'px'
                });
            });
            $(document).mouseup(function () {
                $(document).off('mousemove');
            });
        });
    }
    return dialog;
}

var alertDrawer = function (opts) {
    //设置默认参数
    var opt = {
        'time': 60,
        'msg': '',
        'title': '温馨提示',
        'isHtml': false,
        'butText': '点击查看',
        'timer': null//定时器
    }
    //合并参数
    var option = $.extend(opt, opts);
    var $alertTip = $('<div class="alertTip" id="box"></div>');
    var $alertTip_title = $('<div class="alertTip_title"><div>' + option.title + '</div></div>')
    var $alertTip_close = $('<div class="alertTip_close"><i class="mdi mdi-close"></i></div>')
    var $timing = $('<div class="timing"><span class="seconds">' + option.time + '</span>秒后关闭</div>');
    var $alertBtn = $('<button class="alertTip_Button">' + option.butText + '</button>')
    var $alertTip_footer = $('<div class="alertTip_footer"></div>')
    $alertBtn.bind("click", option.fun);//给按钮添加事件
    var $alertTip_content;
    if (option.isHtml) {
        $alertTip_content = $('<div class="alertTip_content"></div>').append(opts.msg)
    } else {
        $alertTip_content = $('<div class="alertTip_content">' + option.msg + '</div>');
    }
    var alertTip = {}
    alertTip.init = function () {
        $alertTip.append($alertTip_title.append($alertTip_close)).append($timing).append($alertTip_content).append($alertTip_footer.append($alertBtn))
        $("body").append($alertTip);
    }
    alertTip.init()
    alertTip.close = function () {
        clearInterval(option.timer);
        $(".alertTip").remove()
    }
    $alertTip_close.bind("click", function () {
        clearInterval(option.timer);
        alertTip.close()
    })
    alertTip.daojishi = function () {
        var t = option.time;
        option.timer = setInterval(function () {
            $('.seconds').text(t)
            t--;
            if (t < 0) {
                alertTip.close()
                clearInterval(option.timer);
            }
        }, 1000)
    }

    alertTip.daojishi()
    return alertTip;
}

